<template>
  <div class="ui login">
    <h1>{{ msg }}</h1>
    <h2>请使用邮箱登录</h2>
    <div class="ui form login-form">
      <div class="item">
        <label for="email"></label>
        <input id="email" type="text" v-model="email" placeholder="邮箱">
      </div>

      <div class="item">
        <label for="password"></label>
        <input id="password" type="password" v-model="password" placeholder="密码">
      </div>

      <div class="item">
        <p> {{ err }} </p>
      </div>
      <div>
        <button class="ui button green" type="button" v-on:click="login">登录</button>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'login',
  data () {
    return {
      msg: 'golang+vue2',
      email: '',
      password: '',
      err: ''
    }
  },
  methods: {
    login: function () {
      if (!this.email) {
        alert('请输入用户名')
        return false
      }
      if (!this.password) {
        alert('请输入密码')
        return false
      }
      var _this = this
      this.$api.login({email: this.email, password: this.password}).then(function (data) {
        if (data.code === 0) {
          _this.$store.commit('updateUser', data.data.user)
          _this.$store.commit('updateToken', data.data.token)
          _this.$store.commit('updateWs', null)
          _this.$router.push('/')
        } else {
          _this.err = data.msg
        }
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.login {
  position: fixed;
  margin: 100px auto 0 auto;
  text-align: center;
  width: 1040px;
  padding: 10px 60px;
}
.login-form .item {
  margin-bottom: 10px;
}
.login-form input[type="text"],.login-form input[type="password"] {
  width: 300px;
}
</style>
